package body Generic_Functions is

   -- | Sums up the elements of an array of Integers
   function Sum_Arr_Int(Arr_To_Sum: Int_Array) return Integer is
      Sum: Integer;
   begin
      Sum := 0;
      for I in Arr_To_Sum'Range loop
	 Sum := Sum + Arr_To_Sum(I);
      end loop;
      return Sum;
   end Sum_Arr_Int;

   -- | Split up a String into an array of Unbounded_String following pattern from an Int_Array
   function Take_Enregistrements(Decoup_Tab: Int_Array; Str_To_Read: String) return Str_Array is
      Previous, Next : Integer;
      Arr_To_Return  : Str_Array(Decoup_Tab'Range);
   begin
      if Sum_Arr_Int(Decoup_Tab) > Str_To_Read'Length then
	 raise Constraint_Error;
      else
	 Previous := Decoup_Tab'First;
	 Next     := Decoup_Tab(Decoup_Tab'First);
	 for I in Decoup_Tab'Range loop
	    if I /= Decoup_Tab'First then
	       Previous          := Next + 1;
	       Next              := (Previous - 1) + Decoup_Tab(I);
	    end if;
	    Arr_To_Return(I)  := To_Unbounded_String(Str_To_Read(Previous..Next));
	 end loop;
	 return Arr_To_Return;
      end if;
   end Take_Enregistrements;

   -- | Split up String into an array of Unbounded_String following the number and length of parts chosen
   function Split_Str(Str_To_Split: String; Number_Of_Parts: Integer; Length_Of_One_Part: Integer) return Str_Array is
      Arr_To_Return : Str_Array(1..Number_Of_Parts);
      Tmp_Str       : String(1..Length_Of_One_Part);
      Offset        : Integer;
   begin
      if (Str_To_Split'Length mod Length_Of_One_Part) = 0 and (Str_To_Split'Length / Length_Of_One_Part) = Number_Of_Parts then
	 Offset := Str_To_Split'First;
	 for I in Arr_To_Return'Range loop
	    for I in Tmp_Str'Range loop
	       Tmp_Str(I) := Str_To_Split(Offset + I - 1);
	    end loop;
	    Offset := Offset + Length_Of_One_Part;
	    Arr_To_Return(I) := To_Unbounded_String(Tmp_Str);
	 end loop;
	 return Arr_To_Return;
      else
	 raise Constraint_Error;
      end if;
   end Split_Str;

end Generic_Functions;
